﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RobotBattle2.Runtime;

namespace RobotBattle2.Runtime {
	public class VariableScope : IVariableScope {
		public VariableScope() : this(null) { }
		public VariableScope(IVariableScope parent) {
			this.ParentScope = parent;
		}

		VariableCollection locallyDefined = new VariableCollection();
		public IVariableScope ParentScope { get; private set; }

		public virtual IVariable DefineLocal(string name) {
			var variable = new Variable(name);
			locallyDefined.Add(variable);
			return variable;
		}
		public virtual IVariable DefineGlobal(string name) {
			if (ParentScope == null)
				return DefineLocal(name);
			else
				return ParentScope.DefineGlobal(name);
		}
		public virtual IVariable Get(string name) {
			if (locallyDefined.Contains(name))
				return locallyDefined[name];
			else if (ParentScope != null)
				return ParentScope.Get(name);
			else
				return null;
		}
	}
}
